#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名：02-list.py

# 字典中的字符串一般都有''单引号

# 元组（）除了不能更新，其余操作和列表一致
list1 = []
list1.append(1)
list1.append(3)
list1.append('a')
list1.append('b')
list1.append(5)
list1.append(100)

print "打印整个列表"
print list1

print "打印元素3的索引 {0}".format(list1.index(3))
print "打印下标为1的元素 {0}".format(list1[1])

print "打印元素3的次数 {0}".format(list1.count(3))
list1.remove(100)
print "打印删除100后的列表 {0}".format(list1)

list1.insert(5, 100)
print "打印在下标6插入100后 {0}".format(list1)
# 从0开始的下标
pop6 = list1.pop(5)
print "弹出的下标为6的元素 {0} ".format(pop6)
print "弹出下标为6的元素后，列表内容为 {0} ".format(list1)



print "翻转列表,直接对列表进行了操作"
list1.reverse()
print "翻转后的列表{0}".format(list1)



print "打印从下标1到3，不包含3 {0}".format(list1[1: 3])


print "打印从0到3 {0}".format(list1[:3])

print "打印从3到结尾 {0}".format(list1[3:])


print "重复2次{0} ".format(list1 * 2)

list2 = [11,22,"aa"]

print "列表相连"
print list1 + list2

print "用extend"
list1.extend(list2)
print list1
# 成员运算符
print 3 in list1

if 3 in list1:
    print "b 在list1中"
else:
    print "b not in list1"
    
print ('a' in list1)

# 列表转成字符串 ，很常用
fields = ["name", "age"]
fields = ",".join(fields)
print fields


list2 = [11, 22, "aa"]

print list2[:-1]



table = 'test_mysqldb'
attrs = ["id", "name", "age"]
values = [['101', "liuqiao", "25"], ['102', "liuqiao1", "26"], ['104', "liuqiao2", "27"], ['104', "liuqiao3", "28"]]

# 列表拼接成字符串
attrs_sql = '(' + ','.join(attrs) + ')'
#print attrs_sql

values_sql = ' values '
# 遍历列表，取出每一项，也是列表
for v in values:
    # 拼接成字符串
    for v1 in v:
        values_sql = values_sql + '\'' + v1 + '\'' + ','
    values_sql = '(' + values_sql + ')'
    #values_sql = values_sql + '(' + ','.join(v) + '),'
#print values_sql[:-1]
#print values_sql

#  正确的 insert into test_mysqldb(age,sex,name,income)
# values('102','m','caixinglong3',500),('202','m','caixinglong3',500);


'''sql = 'insert into {0}'.format(table)
sql = sql + attrs_sql + values_sql
print 'insertMany:' + sql'''


'''values = ('101', "liuqiao", "25")
print values'''

values = [('101', "liuqiao", "25"), ('102', "liuqiao1", "26"), ('104', "liuqiao2", "27"), ('104', "liuqiao3", "28")]


values_sql = ' values '
    # 遍历列表，取出每一项，也是列表
for v in values:
        # 拼接成字符串
    values_sql = values_sql + str(v) + ","
print values_sql[:-1]

    #  正确的 insert into test_mysqldb(age,sex,name,income)
    # values('102','m','caixinglong3',500),('202','m','caixinglong3',500);
    
tuple1 = ('101', 'liuqiao', '25')
print type(tuple1)
print type(str(tuple1))